<?php use_helper('I18N') ?>
<?php if(!isset($ajaxSearch)) $ajaxSearch = false;?>

<script type="text/javascript">
/*
 * calls the renderSearchBar action and shows the basic search form in the search div
 *
 */

function reRenderSearchBar(){
	var xhrSearch = getXmlHttpRequest2();
	var fieldName = '';
	var operator = '';
	if(document.getElementById('wrapper_from_until')){
		document.getElementById('wrapper_from_until').innerHTML = '<img style="width:50%;height:25px;" alt="wait please..." src="/images/ajax-loader.gif">';
	}
	xhrSearch.open("POST","<?php print url_for('search/RenderSearchBar'); ?>",false);
	xhrSearch.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	var fieldName='';
	if(document.getElementById('qff_fieldName')){
		field=document.getElementById('qff_fieldName');
		fieldName=field.options[field.selectedIndex].value;
	}
	xhrSearch.send("className=<?php echo $className?>&fieldName="+fieldName);
	var xHtml = xhrSearch.responseText;
	document.getElementById('search').innerHTML = xhrSearch.responseText;
	var search_field_type = document.getElementById('search_field_type').value;
	if (search_field_type == 'date' || search_field_type=='timestamp'){
		if(search_field_type=='timestamp'){
			var timestamp = true;
		}else{
			var timestamp = false;
		}
		if('<?php echo sfConfig::get('app_form_datewidgetclass', 'sfWidgetFormDateJS');?>'=='sfWidgetFormDateJQueryUI'){
			setJqueryDatePicker('qff_valueFrom', '<?php echo $sf_user->getCulture()?>', timestamp, false, false, true, 1, '/images/icons/cal.png');
			setJqueryDatePicker('qff_valueUntil', '<?php echo $sf_user->getCulture()?>', timestamp, false, false, true, 1, '/images/icons/cal.png');
		}
	}
}

/*
 * calls the renderAdvancedSearch action and shows the advanced search form in the advancedFilterForm div
 *
 */

function showAdvancedSearch(){
	var xhrSearch = getXmlHttpRequest2();
	var fieldName = '';
	var operator = '';
	var field=document.getElementById('aff_fieldName');
	if(field){
		fieldName=field.options[field.selectedIndex].value;
	}
	var field=document.getElementById('aff_operator');
	if(field){
		operator=field.options[field.selectedIndex].value;
	}
	xhrSearch.open("POST","<?php print url_for('search/renderAdvancedSearch'); ?>",false);
	xhrSearch.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrSearch.send("className=<?php echo $className?>&fieldName="+fieldName+"&operator="+operator);	
	var xHtml = xhrSearch.responseText;
	document.getElementById('advancedFilterForm').innerHTML = xhrSearch.responseText;
}

function closeAdvancedSearch(){
	document.getElementById('advancedFilterForm').innerHTML = '';
}

function enlarge(id){
	document.getElementById(id).style.width = '300px';
}
function ensmall(id){
	document.getElementById(id).style.width = '';
}

function addFilters(formName){
	if(!formName){
		formName = 'searchForm';
	}
	//var elemIF = document.createElement("iframe");
	var elemIF = document.getElementById('filtersAdd');
	//elemIF.src = ''; 
	elemIF.id = 'filtersAdd';
	elemIF.name = 'filtersAdd';  
	//elemIF.style.display = "none"; 
	//document.body.appendChild(elemIF);
    
	
	if(!document.getElementById(formName)){
		reRenderSearchBar();
	}
	var f = document.getElementById(formName); 
	f.method = 'post'; 
	f.action = '<?php print url_for('search/addFilters'); ?>';
	f.target = 'filtersAdd';
	if(f.elements['searchformName']){
		f.elements['searchformName'].value = formName;
	}else{
		var m = document.createElement('input'); 
		m.setAttribute('type', 'hidden'); 
		m.setAttribute('name', 'searchformName'); 
		m.setAttribute('id', 'searchformName');
		m.setAttribute('value', formName);
		f.appendChild(m);
	}
	if(!f.elements['_csrf_token']){
		var m = document.createElement('input'); 
		m.setAttribute('type', 'hidden'); 
		m.setAttribute('name', '_csrf_token'); 
		m.setAttribute('id', '_csrf_token'); 
		m.setAttribute('value', 'e0b25fb2cfea7fb6b134f5fe808c2b62'); 
		f.appendChild(m);
	}
	if(!f.elements['className']){
		var m = document.createElement('input'); 
		m.setAttribute('type', 'hidden'); 
		m.setAttribute('name', 'className'); 
		m.setAttribute('id', 'className');
		m.setAttribute('value', '<?php echo $className?>');
		f.appendChild(m);
	}	
	if(document.getElementById('refreshFunctionParams')){
		var params = document.getElementById('refreshFunctionParams');
		var m = document.createElement('input'); 
		m.setAttribute('type', 'hidden'); 
		m.setAttribute('name', 'refreshParams'); 
		m.setAttribute('id', 'refreshParams');
		m.setAttribute('value', params.value);
		f.appendChild(m);
	} 
	f.submit();

    
}

function showFilterWarning(msg){
	if(msg==''){
		document.getElementById('activeFilters').innerHTML =  '';
		document.getElementById('activeFilters').style.display='none';
	}else{
		document.getElementById('activeFilters').innerHTML =  '<div>'+msg+'</div>';
		document.getElementById('activeFilters').style.display='block';
	}
}

function clearFilters( filterID){
    var f = document.createElement('form'); 
	f.style.display = 'none'; 
	document.body.appendChild(f); 
	f.method = 'post';
	f.action = '<?php print url_for('search/RemoveFilters'); ?>';
	f.target = 'filtersAdd';
	var m = document.createElement('input'); 
	m.setAttribute('type', 'hidden'); 
	m.setAttribute('name', '_csrf_token'); 
	m.setAttribute('id', '_csrf_token'); 
	m.setAttribute('value', 'e0b25fb2cfea7fb6b134f5fe808c2b62'); 
	f.appendChild(m);
	var m = document.createElement('input'); 
	m.setAttribute('type', 'hidden'); 
	m.setAttribute('name', 'classname'); 
	m.setAttribute('id', 'classname');
	m.setAttribute('value', '<?php echo $className?>');
	f.appendChild(m);
	if( filterID!=''){
		var m = document.createElement('input'); 
		m.setAttribute('type', 'hidden'); 
		m.setAttribute('name', 'id'); 
		m.setAttribute('id', 'id');
		m.setAttribute('value', filterID);
		f.appendChild(m);
	}
	if(document.getElementById('refreshFunctionParams')){
		var params = document.getElementById('refreshFunctionParams');
		var m = document.createElement('input'); 
		m.setAttribute('type', 'hidden'); 
		m.setAttribute('name', 'refreshParams'); 
		m.setAttribute('id', 'refreshParams');
		m.setAttribute('value', params.value);
		f.appendChild(m);
	} 
	f.submit();
	
}

/*
 * calls the renderAdvancedSearch action and shows the advanced search form in the advancedFilterForm div
 *
 */

function showSaveForm(filter_id){
	var xhrSearch = getXmlHttpRequest2();
	var action_module = "<?php echo $sf_context->getModuleName() . '/' . $sf_context->getActionName();?>";
	
	xhrSearch.open("POST","<?php print url_for('search/renderSaveSearch'); ?>",false);
	xhrSearch.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrSearch.send("filter_id="+filter_id + "&action_module=" + action_module);	
	var xHtml = xhrSearch.responseText;
	document.getElementById('saveFiltersDiv').innerHTML = xhrSearch.responseText;
}

function closeSaveSearch(){
	document.getElementById('saveFiltersDiv').innerHTML = '';
}

function applySavedSearch(){
	var xhrSearch = getXmlHttpRequest2();
	xhrSearch.open("POST","<?php print url_for('search/renderApplySearch'); ?>",false);
	xhrSearch.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrSearch.send("className=<?php echo $className?>");	
	var xHtml = xhrSearch.responseText;
	document.getElementById('applySearchDiv').innerHTML = xhrSearch.responseText;
	if(document.getElementById('refreshFunctionParams')){
		var f = document.getElementById('applySearchForm');
		var params = document.getElementById('refreshFunctionParams');
		var m = document.createElement('input'); 
		m.setAttribute('type', 'hidden'); 
		m.setAttribute('name', 'refreshParams'); 
		m.setAttribute('id', 'refreshParams');
		m.setAttribute('value', params.value);
		f.appendChild(m);
	}
}

function closeApplySearch(){
	document.getElementById('applySearchDiv').innerHTML = '';
}

/* 
 * 
 * Function to hide or show roles in the saveSearch form when public is choosen.
 * 
 */
function toogleRoles(){
	if(document.getElementById('filterdef_public').checked)
	{
		document.getElementById('wrapperroles').style.display = '';
	}
	else
	{
		document.getElementById('wrapperroles').style.display = 'none';
	}
}

function deleteSavedSearch(filter_id){
	if (confirm(__('Are you sure?'))) {
		var xhrSearch = getXmlHttpRequest2();
		xhrSearch.open("POST","<?php print url_for('search/deleteSavedSearch'); ?>",false);
		xhrSearch.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhrSearch.send("filter_id="+filter_id);
		clearFilters('');	
	}
}
function saveFilterDef(){
	var f = document.getElementById('Formfilterdef');
	f.submit();
}

</script>
	<div id="search"></div>
	<div id="advancedFilterForm"></div>
	<iframe id="filtersAdd" name="filtersAdd" style="width:0;height:0;border:0px solid #fff;"></iframe>
	<div id="activeFilters" class="filterDisplay" style="display:none"></div>
	<div id="saveFiltersDiv"></div>
	<div id="applySearchDiv"></div>
	
<script type="text/javascript">
	//reRenderSearchBar();
</script>